package com.wbc.smartHome.web;

import com.github.pagehelper.PageInfo;
import com.wbc.smartHome.model.CommunityActivities;
import com.wbc.smartHome.model.CommunityActivitiesLabel;
import com.wbc.smartHome.model.Result;
import com.wbc.smartHome.service.CommunityActivitiesService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping(path = "/api/communityActivitiesCtl")
/*社区活动存取*/
@Api(tags = "社区活动模块")
public class CommunityActivitiesController {


    @Autowired
    private CommunityActivitiesService communityActivitiesService;

    @ApiOperation("获取全部社区活动")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getCommunityActivitiesList")
    public Result getCommunityActivitiesList() {
        List<CommunityActivities> communityActivitiesList = communityActivitiesService.getCommunityActivitiesList();
        return new Result(200, "OK", communityActivitiesList);
    }

    @ApiOperation("添加社区活动")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @PostMapping(path = "setCommunityActivities")
    public Result setCommunityActivities(@RequestBody CommunityActivities communityActivities,@RequestHeader("adminToken") String adminToken) {
        if (communityActivities.getId()==null){
            communityActivitiesService.setCommunityActivities(communityActivities,adminToken);
        }
        else {
           communityActivitiesService.updateCommunityActivities(communityActivities,adminToken);

        }
        return new Result(200,"ok",null);
    }

    /*通过标签id获取活动*/
    @ApiOperation("通过标签id获取社区活动详细信息")
    @ApiImplicitParam(name = "labelId",value = "标签id", required = true,type = "Integer")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getCommunityActivitiesLabelNameByLabelId")
    public Result getCommunityActivitiesLabelNameByLabelId(Integer labelId) {
        List<CommunityActivities> communityActivitiesList = communityActivitiesService.getCommunityActivitiesLabelNameByLabelId(labelId);
        return new Result(200,"OK",communityActivitiesList);
    }

    /*通过id查询活动详细信息并将标签查询出来封装进商品*/
    @ApiOperation("通过活动id获取社区活动详细信息")
    @ApiImplicitParam(name = "id",value = "活动id", required = true,type = "Integer")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getCommunityActivitiesById")
    public Result getCommunityActivitiesById(@RequestParam("id") Integer id) {
        CommunityActivities communityActivities =communityActivitiesService.getCommunityActivitiesById(id);
        return new Result(200,"OK",communityActivities);
    }

    /*活动推荐*/
    @ApiOperation("活动推荐")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getRecommendCommunityActivities")
    public Result getRecommendCommunityActivities() {
        List<CommunityActivities> communityActivitiesList = communityActivitiesService.getRecommendCommunityActivities();
        return new Result(200,"OK",communityActivitiesList);
    }

    @ApiOperation("后台获取全部社区活动")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @PostMapping(path = "CommunityActivitiesList")
    public Result CommunityActivitiesList(@RequestBody CommunityActivities communityActivities) {
        PageInfo<CommunityActivities> list  = communityActivitiesService.communityActivitiesList(communityActivities);
        return new Result(200,"OK",list);
    }

    @ApiOperation("上传类型封面图")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @PostMapping("/uploadImg")
    public Result uploadImg(@RequestParam("file") CommonsMultipartFile commonsMultipartFile) throws IOException {
        Result result = communityActivitiesService.uploadImg(commonsMultipartFile);
        return  result;
    }

    @ApiOperation("删除图片")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "deleteImg")
    public Result deleteImg(String imgurl) throws IOException {
        communityActivitiesService.deleteImg(imgurl);
        return new Result(200,"删除成功",null);
    }

    @ApiOperation("删除活动")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "删除成功"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "delete")
    public Result delete(int id){
        communityActivitiesService.delete(id);
        Result result = new Result(200, "删除成功", null);
        return result;
    }

    @ApiOperation("获取所有活动标签")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "删除成功"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getCommunityActivitiesLabelList")
    public Result getCommunityActivitiesLabelList(){
        List<CommunityActivitiesLabel> communityActivities = communityActivitiesService.getCommunityActivitiesLabelList();
        return new Result(200,"OK",communityActivities);
    }


}
